<template>
  <div class="container">
    <el-table
      :data="paginatedData"
      border
      class="custom-table"
      style="width: 100%"
    >
      <el-table-column prop="courseId" label="课程ID" width="100" />

      <el-table-column label="最多预约人数" width="140">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.maxReservations"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.maxReservations }}</span>
        </template>
      </el-table-column>

      <el-table-column label="最少预约人数" width="140">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.minReservations"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.minReservations }}</span>
        </template>
      </el-table-column>

      <el-table-column label="预约可提前(天)" width="150">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.reservationWindow"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.reservationWindow }}</span>
        </template>
      </el-table-column>

      <el-table-column label="取消截止(小时)" width="150">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.cancelDeadline"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.cancelDeadline }}</span>
        </template>
      </el-table-column>

      <el-table-column label="超时自动取消" width="140">
        <template #default="{ row }">
          <el-switch v-if="row.editing" v-model="row.autoCancelNoPayment" />
          <span v-else>{{ row.autoCancelNoPayment ? "是" : "否" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付超时(分)" width="150">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.paymentTimeout"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.paymentTimeout }}</span>
        </template>
      </el-table-column>

      <el-table-column label="允许候补" width="120">
        <template #default="{ row }">
          <el-switch v-if="row.editing" v-model="row.allowWaitlist" />
          <span v-else>{{ row.allowWaitlist ? "是" : "否" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="候补上限" width="120">
        <template #default="{ row }">
          <el-input
            v-if="row.editing"
            v-model="row.waitlistCapacity"
            size="small"
            type="number"
            class="small-input"
          />
          <span v-else>{{ row.waitlistCapacity }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-switch v-if="row.editing" v-model="row.status" />
          <span v-else>{{ row.status ? "启用" : "禁用" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        fixed="right"
        width="180"
      >
        <template #default="{ row }">
          <template v-if="row.editing">
            <el-button size="small" type="primary" @click="saveRow(row)">保存</el-button>
            <el-button size="small" @click="cancelEdit(row)">取消</el-button>
          </template>
          <template v-else>
            <el-button size="small" type="primary" @click="editRow(row)">编辑</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="settings.length"
        :page-size="pageSize"
        v-model:current-page="currentPage"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const settings = ref([])
const currentPage = ref(1)
const pageSize = 9

const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  return settings.value.slice(start, start + pageSize)
})

const fetchSettings = async () => {
  try {
    const res = await axios.get('http://localhost:8080/api/reservation-settings')
    settings.value = res.data.map(item => ({
      ...item,
      editing: false,
      original: { ...item }
    }))
  } catch (error) {
    ElMessage.error('加载预约设置失败')
  }
}

const editRow = row => {
  row.editing = true
}

const cancelEdit = row => {
  Object.assign(row, row.original)
  row.editing = false
}

const saveRow = async row => {
  try {
    const { settingId, ...dataToUpdate } = row
    await axios.put(`http://localhost:8080/api/reservation-settings/${settingId}`, dataToUpdate)
    row.original = { ...row }
    row.editing = false
    ElMessage.success('保存成功')
  } catch (err) {
    ElMessage.error('保存失败')
  }
}

onMounted(fetchSettings)
</script>

<style scoped>
.container {
  padding: 20px;
  margin-top: 10px;
}

.custom-table {
  font-size: 16px;
}

.custom-table .el-table__body,
.custom-table .el-table__header {
  font-size: 16px;
}

.small-input .el-input__inner {
  width: 80px;
  padding: 0 6px;
  height: 28px;
  font-size: 15px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
